package controller;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import model.gameModel;
import model.gameModelI;

import view.gamePanel;
import view.instrPanel;
import view.leaderBoardPanel;
import view.titlePanel;

public class screenController extends JFrame {

	private static final long serialVersionUID = 6977576048557950634L;
	private gameModelI model;
	private logController controlLogs;
	private userInputController controlInput;
	private gamePanel game;
	private leaderBoardPanel leader;
	private instrPanel instr;
	private titlePanel title;
	private JPanel currentPanel;
	
	public screenController() {

		super("Word Jump");
		model = new gameModel(); 
		controlInput = new userInputController(model, null, this);
    
		game = new gamePanel(model, controlInput); //needs to know user input controller 
		title = new titlePanel();
	    instr = new instrPanel();
	    leader = new leaderBoardPanel(model);
		    
	    title.addPlayListener(new PlayListener(game));
	    title.addLeaderBoardListener(new switchPanelListener(leader));
	    title.addInstrListener(new switchPanelListener(instr));
	    instr.addInstrReturnListener(new instrSwitchPanelListener(instr, title));
	    instr.addNextListener(new switchImageListener(instr));
	    leader.addReturnListener(new switchPanelListener(title));
	    game.addQuitListener(new QuitGameListener(title));
	    game.addLeaderboardListener(new QuitToLeaderListener(leader));
	    
	    controlLogs = new logController(model, game, controlInput);
	
	    controlInput.setView(game);
	    
	    //setup JFrame             
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		setSize(1000, 600);
		
		changePanel(title) ;
		
		pack();
		setVisible(true);
		setResizable(false);
	}


	private void changePanel(JPanel panel) {
		if(currentPanel != null)
		    getContentPane().remove(currentPanel); 
		
		getContentPane().add(panel);
		currentPanel = panel;
		currentPanel.requestFocus();
		validate();
		repaint();
	}
	
	public void endGame() {
		if(currentPanel != null)
		    getContentPane().remove(currentPanel); 
		controlLogs.stop();
		controlInput.reset();
		changePanel(leader);
	}
		
	
	class PlayListener implements ActionListener {
		private JPanel panel;
	    private PlayListener(JPanel pnl) {
	        this.panel = pnl;
	    }
		
		public void actionPerformed(ActionEvent e) {
			changePanel(panel);
			model.setupGame();
			controlLogs.start();
		}
	}
	
	class switchPanelListener implements ActionListener {
		private JPanel panel;
	    private switchPanelListener(JPanel pnl) {
	        this.panel = pnl;
	    }
		
		public void actionPerformed(ActionEvent e) {
			changePanel(panel);
		}
	}
	
	class instrSwitchPanelListener implements ActionListener {
		private instrPanel panel;
		private JPanel nextPanel;
	    private instrSwitchPanelListener(instrPanel pnl, JPanel n) {
	        this.panel = pnl;
	        this.nextPanel = n;
	    }
		
		public void actionPerformed(ActionEvent e) {
			panel.resetButtons();
			changePanel(nextPanel);
		}
	}
	
	class switchImageListener implements ActionListener {
		private instrPanel panel;
	    private switchImageListener(instrPanel pnl) {
	        this.panel = pnl;
	    }
		
		public void actionPerformed(ActionEvent e) {
			panel.switchBkgd();
		}
	}
	
	class QuitGameListener implements ActionListener {
		private JPanel panel;
	    private QuitGameListener(JPanel pnl) {
	        this.panel = pnl;
	    }
		
		public void actionPerformed(ActionEvent e) {
			controlLogs.stop();
			controlInput.reset();
			changePanel(panel);
		}
	}
	
	class QuitToLeaderListener implements ActionListener {
		private JPanel panel;
	    private QuitToLeaderListener(JPanel pnl) {
	        this.panel = pnl;
	    }
		
		public void actionPerformed(ActionEvent e) {
			controlLogs.stop();
			controlInput.reset();
			String name = null;
			if (model.isHighScore()) {
				name = JOptionPane.showInputDialog(null, "You reached a new high score! Please enter your name: ");
				model.setHighScore(name); //a way to cancel
			}
			changePanel(panel);
		}
	}

	
	public static void main(String[] args) {
		SwingUtilities.invokeLater( new Runnable() {
			@Override public void run() {
				new screenController();
			}} ) ;
	}
	
}